package com.zhangx.datastructure.search;

/**
 * @ClassName Sequential
 * @Description 顺序查找
 * @Author zhangx
 * @Date 2021/1/22 8:46
 **/
public class Sequential {

    public int[] a = new int[]{0,1,9,6,7,2,3,5};

    /**
     * 设置哨兵查找法
     * 跟for 循环相比少了一次比较，量大才有优势，复杂度O(n)
     * @return
     */
    int sequentialSearch(int[] arr,int key){
        arr[0] = key;//哨兵
        int i = arr.length-1;
        while (arr[i]!=key){

            i--;
        }
        return i;
    }

    public static void main(String[] args) {
        Sequential sequential = new Sequential();
        int index = sequential.sequentialSearch(sequential.a,3);
        System.out.println(index);
    }
}
